clear all
use  "../data/zip_year_data", clear

*keep only the years of the shock
keep if year > 2007

*Define CHTV and CHTC  and the percent of overall transaction value and count
gen chtc = (chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash))
gen chtv = (chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval))

gen chinese_cash_percent = chtv/sum_val_tran * 100
gen chinese_cash_ct_percent = chtc/ct * 100

tempfile tmp
save `tmp', replace

*****First do sampling for values
*Create a blank dataset that will be appended on over and over
clear all
gen decile = .
tempfile val
save `val', replace

*sample decile by decile
forvalue i = 1/10{
use `tmp', clear
keep if decile == `i'

*sample with replacement 1000 times
forvalue z = 1/1000{
preserve
local y = _N
bsample `y'
collapse (mean) chinese_cash_percent [w=sum_val_tran], by(decile)
append using `val'
save `val', replace
restore
}
}
use `val', clear

sort decile_chi_pop

*calculate the mean and 5th/95th percentile
forvalues i=1/10 {
	di `i'
	sum chinese_cash_percent if decile_chi_pop==`i',detail
	matrix mat`i' = (r(mean) \r(p5) \r(p95))
    	svmat mat`i'
}
keep mat11-mat101
drop if mat11==.
gen marker=_n
reshape long mat, i(marker) j(decile)
reshape wide mat, i(decile) j(marker)
replace decile=floor(decile/10) //get rid of the 1 in suffix
ren mat1 v
ren mat2 lb
ren mat3 ub
twoway (line lb decile, lpattern(shortdash) color(red)) (line ub decile, lpattern(shortdash) color(red)) (line v decile, color(red)), ///
	xtitle("Chinese Population Share in 2000, by Decile",size(7pt)) ytitle("Chinese/Total Housing Transaction Value (%)",size(7pt)) xlab(1(1)10,labsize(small)) ylab(1(1)6,labsize(small)) leg(off)  ///
	graphregion(color(white)) bgcolor(white) ///
        ylabel(,nogrid) 
graph export "../results/figure2a.png", replace


*****Now do it for counts
*Create a blank dataset that will be appended on over and over
clear all
gen decile = .
tempfile ct
save `ct', replace

*sample decile by decile
forvalue i = 1/10{
use `tmp', clear
keep if decile == `i'

*sample with replacement 1000 times
forvalue z = 1/1000{
preserve
local y = _N
bsample `y'
collapse (mean) chinese_cash_ct_percent [w=ct], by(decile)
append using `ct'
save `ct', replace
restore
}
}
use `ct', clear

sort decile_chi_pop

*calculate the mean and 5th/95th percentile
forvalues i=1/10 {
	di `i'
	sum chinese_cash_ct_percent if decile_chi_pop==`i',detail
	matrix mat`i' = (r(mean) \r(p5) \r(p95))
    	svmat mat`i'
}
keep mat11-mat101
drop if mat11==.
gen marker=_n
reshape long mat, i(marker) j(decile)
reshape wide mat, i(decile) j(marker)
replace decile=floor(decile/10) //get rid of the 1 in suffix
ren mat1 v
ren mat2 lb
ren mat3 ub
twoway (line lb decile, lpattern(shortdash) color(red)) (line ub decile, lpattern(shortdash) color(red)) (line v decile, color(red)), ///
	xtitle("Chinese Population Share in 2000, by Decile",size(7pt)) ytitle("Chinese/Total Housing Transaction Count (%)",size(7pt)) xlab(1(1)10,labsize(small)) ylab(1(1)6,labsize(small)) leg(off)  ///
	graphregion(color(white)) bgcolor(white) ///
        ylabel(,nogrid) 
graph export "../results/figure2b.png", replace
